working search algorithm