updated loop structure