Clean up and reorganize main.js