Reading and writing from templates

Lots more updates - see commits for details.