updated search algorithm in main2