Refactoring tests and code