optimize search code