I didn't actually change the `foreach` block, I just removed it from the `if` block.
I'd prefer to continue any further refactoring in a separate PR to properly fix #396.