minor refactoring and fixes