Final version of search function, still needs to be tested though