Fixed an memory leak and bug.