Optimization and refactoring