A bunch of refactoring. Code is starting to look a lot nicer.