Minor refactoring and optimizing.