added tests and started refactoring