more refactoring and code cleanups