Changed var scope

Added some comments explaining scope too