Moved processing of search terms to a function for reuse