Optimized the code extremely