Minor refactoring, unit test fixing