some refactoring and tests
Fixed the tests