Minor refactoring and improvements.