Improve error handling in Template class.
Add Template class tests.