Refactoring and tests
Some tests fixed
Tests fixed