more tweaks and refactoring