File write bug fix