Improved search logic