Restructured templates to avoid stack overflow