I re-wrote the function to compute the objective function and its derivative after a bit flip.  The purpose was to make it more readable (so I can more readily extend it) and to make it take the model structs.