refactoring and optimizations