Lots of enhancements to template expansion, function Ted::expand