moved the iterator keys lookup code out of the loop

Optimization fix as requested