updated with tests and refactoring
Merging branches