Fixed some potential memory leaks as well as issues with file processing.