Added logic to write files