Refactoring, and a few more tests.