refactor search functionality