Fix implementation of write_file