Move a couple funcs outside of loops

Will cut down on the performance hit.

Not actually tested.  @daniel229, these work right?

-[Unknown]