Specialize branched loops

Continuation of PR #469 inside the main repo.
This is a great idea. I knew about this function, but forgot to use it here. I pushed some commits that address this and your other comments above.