more refactoring and fixes