Removed global variables from functions

- I now feed all non-local variables to the function via arguments,
rather than using them explicitly inside the function.
- This is in service of easily re-using these functions in other
scripts or creating a function library for future projects.