move conditional out of loop