Fixed bug in loop. 8/10 test cases working.