A bit of refactoring, got tests passing.