Fixing potential crashes and memory leaks.