Added more unit test coverage; refactorings