modified slightly the code to overcome difficulties when saving templates-now solved