See if replacing the while / each loop with a foreach allows duplicates to get processed.