nested for loops - almost works