Small refactoring and optimizations