Fixed potential null pointer dereference.
Removed no-ops (variable set but never read).
Fixed improper type declaration in malloc.
Fixed several potential null pointer dereferences.