Improve set finding function with caching