Factored code for global JS variables into a function