replaced txt i/o with csv, cleaned up some code