dramatic restructuring of main.js to make it more object-oriented and easier to read