Fix file reading and writing