Fully implemented chi2 functionality