added a better optimize search function