More refactoring of main.js