Further refactoring and optimizations
Refactoring and optimization