more refactoring and minor changes