mid way refactoring
unit tests, bug fixes