created longest_match method