restructuring js

to help scope problem