Minor refactoring and style fixes