Update and refactor search code