Revised the loop structure