Fix search algorithm for edge case