Templates rewrite.

Rewrote it to use templates instead of a separate function per data type.