modified to write files in multiple directories