optimize search algorithm