Misc. refactoring and style fixes