Refactor js

Rewriting our js to be more readable and less hacky.


This can be a lot more concise.