Fixed Memory Leaks due to error in between of search operation and some code improvements