Improve error messages.
Major cleanup to template script.