Moved a few functions around

Moved template functions so the definitions are before the
use.