Fixed loop bug in seacrh()