Working out While and nested loop issues