added a recursive algorithm to search for the first match