copied over Andrew's model fitting work