Fixed a potential exception. Close files when done.