Minor refactoring for testability and clarity.