optimized the code for unwanted loops