refactored version with some TDD