Refactored code for better testability