Refactoring to make a method easier to test