fixed bug in template expansion