added logic to write files