Dead lock and Memory leak fixes