refactoring and fixes