move check out of the loop

move check out of the loop