Searching algorithm and code