Refactoring to get it passing in tests.