fix file writing of correct file