Specialize branched loops

Continuation of PR #469 inside the main repo.
I have no idea why I added that. Does something break without it?