Moved the model fitting code into a cpp file.