more refactoring and cleanups, bug fixes