Updated code and tests to address looping error