Fixed bugs in the template handling.