Fixed potential memory leaks and crash.