Performance refactoring and lots of tests