Lots and lots of refactoring