Use correct flags when opening files for writing