Refactor main js file