[WIP] Refactor two-iteration anti-pattern code in spell.c

Start refactoring spell.c, starting with converting code using the two-iteration anti-pattern as described in #572.

I'll probably look into more refactoring and clean up while I'm at it.