added code for 2nd and 3rd degree model fitting