Added writing data to file. Remove debug statements.