fixed nested for loops, and added more tests