added writing to files at end