Added more unittests and some minor refactoring