changed loop order