move some logic out of the loop that wasn't meant to be in there