Preliminary refactoring to allow some test coverage before changing behaviour.