Refactor main.js, and start cleanup