move check out of the loop