After days of frustration, I have written a way to automate localization of variables declared inside the code, however due to quirks of the Wolfram language it appears I cannot make it into a function. Thank you though.
This seems to be answered here:
but often all you need is to write packages:
Creating Mathematica packages
Scoping all symbols