more refactoring and cleanups