Modified to write data to a local directory