Refactored for testability and readability