Fixed the memory leaks.