Fixed an issue with the file I/O. Also fixed a minor memory leak.