refactored for better testability