Fix linear search algorithm to avoid unnecessary checks