Lots of tests and refactoring!