Fully working program done, including chi^2.