various cleanups and refactoring