Minor refactoring and optimization.