Simplify several loops. (similar to 9abcda1)