Improved search function