added ability to write to file and stop reactor when everything is finished