Rewrite of the basic search function

Updated the search function to something easier to understand, and
should be faster