Refactor search field code