Refactorizing code and tests