Cleanup - remove type from local functions, use global variable instead
Cleanup, chi-square calculation