Fix crash when using all batteries (#674)

Note to self: when decrementing an STL iterator inside nested loops, use "break" rather than trying to reset the conditions of the inner loop. Less likely to crash that way.

Closes #674.