Small refactoring and optimizations.