rstemplatematching2: working implementation