changed loop structure