Minor refactoring and some tests.