Refactored sampleSearch code