Bug fix to template expansion