Refactoring to make things more unit-testable