optimized the original function