Some small refactorings. Additional unit tests.