Fixed test, cover more tests and small refactoring