working algorithm, csv file saving not yet complete