JS Refactors

Refactor Javascript to be cleaner and better organised.