Factoring out search code.