C++: much improved and simplified the search algorithm.