Perry,
It was the precision you were trying to do that was causing the problems. The SCAN loop does certain things for you, but you were overriding some of them.
A major key to debugging that many folks forget is to understand clearly why it doesn't work before yout ry to fix it. The ohter approach, adding fixes without understanding why it isn;t working just causes a "schewing gum and spit" solution that will be riddled with situations that are not clearly defined and add to the difficulty of future debugging.
The UT can be a great tool for getting an understanding of why something doesn't work and ideas of what might work in its place.