Code cleanup and minor error corrections

Most changes are about language strictness and compiler warnings.
A possible memory corruption was also fixed (caused by not checking malloc() return value).