Refactor JS to reduce global namespace pollution.

Just refactored JS code to reduce global namespace pollution (and cleaned syntax a little bit).