Refactoring plus extra tests