Refactoring, fixing tests