refactor search/replace code