Fixed several memory bugs.