moving search logic to a function