refactoring for better performance and scalability