write to file properly