Move the patient search code into its own helper function for reusability.