move logic to inner loop

Signed-off-by: Stephen Gran <steve@lobefin.net>