Clean-up of JS files.

This patch splits up main.js into functionalities for clarity.
(the file was getting huge)

I also formatted code - if you want me to keep code as-is, please
leave a comment - right now I'm using tabs as indents, let me know
if you guys prefer something else!