Tidied up template creation code.