Tricky nested for-s :) Can we simplify these or change to while?