Refactored code with improved test coverage.