refactored for better testability.