Fix bug in template code