More refactoring to break logic into testable chunks