Memory leak and NULL dereference fixes.