Optimized search function