Optimized the existing code