refactoring and fixes in implementation