Refactor main js