Updates to fix tests.

Some loop control variables needed to be renamed due to
nested loops.  Also, some of the arrays may not be defined
at all, so we can't rely on always being able to call Array.length.