fix logic bug in template branch matching