Fixed error in formula and writing csv file