In #11005 @L-as introduced c++20 coroutines in order to clean up this code. See that PR for details.
That PR set up the infrastructure to do so, but for most goal types intentionally didn't actually do the simplification. (The simplification was instead left for follow-up PRs, to keep things reviewable.)
This PR tracks finishing the job.