Misc. refactoring and cleanup