Some refactoring, unit tests.