Small refactoring and optimization