Refactoring with better tests