Update code to add chi-squared calculation