Optimization of search algorithm