EUREKA!
I figured it out! There was a line of code missing that defines a variable which is used in a preceding loop. The variable was never declared anywhere in the subroutine but somehow the code went through the loop possibly using a previously defined value of the variable stuck somewhere in memory space. Now my tests work again! Yay!