code refactoring and performance optimizations