working version of search function