Cleanup: moved objective function and related calculation into separate struct.