fixed search algorithm, simplified code