Fixed a few more memleaks. Added a few more checks when handling files.

Fixed a few more memleaks. Added a few more checks when handling files.