Refactoring to allow unit testing