Added applyTemplate function