Fix: correct file closing

Fixed a bug in which the files were being closed improperly, and
would crash the program at the end of its execution.