did a bit of refactoring, hopefully making things more testable.