Bulk of template code