optimize searching algorithm