Lots of test and refactorings