Fixes global variable creation

Some globals were being created here.
I noticed that multiple "var" declarations were being used on other parts of this code os using this instead of one "var" for all the local vars.