Big cleanup

Split apart giant main.js file into separate components while preserving
scope as best as possible. Still a bit of a mess but much more
manageable now.