fixed unit test + a bit of refactoring