refactor search code and escape special characters in search terms