Refactor js

Rewriting our js to be more readable and less hacky.


[PEP8](http://legacy.python.org/dev/peps/pep-0008/#imports) says standard library imports go at the top.