Refactoring and more unit tests