Logic bug. Hoist it out of the loop.