fixed find_pair logic