WIP: Add some template magic.

Another attempt to suppress memory related errors.