Refactoring JS to be more modular