Ensure files are written before closing