modified to a better way of writing files