Small refactoring and performance improvements