refactor search code