Miscellaneous refactoring and cleanups