More speed improvements to range iteration
Cache global variables that are accessed in tight loops