Moving to an IIFE to store some local vars.
Caching various locals that don't need to be recalculated.
Updating one more minor function.