Unit testing

A whole lot of stuff, but mostly added test suites, and shuffled around the controller by putting everything in functions, to make modifications easier.