Minor refactorings and unit tests!