Added more tests and minor refactoring