YJ- fixed bugs in the search algorithm