Refactor the search code in preparation for multi-searches