More unit tests written and some refactoring