Some refactoring and tests.