Adjusted search function to work properly with caching