Added error handling to nth_match.