Final commit. TODO: fix mem leaks with ptr tmplts