some implementation and small refactoring