refactoring some of the code, will write more unit tests and try functional tests soon.