Several patches and bug fixes to templates.