Refactoring and fixed failing tests