Partially solved the memory leak problem.