Refactored the search method