Fixed memory leaks in error branches. Identified by cppcheck tool.
Added missing newlines to end of file, to fix complier warnings.
Replaced strange cast of 0 with NULL constant.
Fixed file handle leaks in error branches. Identified by cppcheck tool.
Marked private functions as static to fix compiler warnings.