Fix stack overflow error when extracting template.