a lot of work in template.c