fixed script to write correct data to file