refactoring and minor tweaks