Fix JsHint warnings, including a minor refactor for readability along the way.
JsHint fixes: use camel case for global function, and put it on window to avoid 'not defined' warning