Mostly got it working! Successfully deduped using Set. 

The only problem remaining is that there are sometimes duplicate arrows for the same image, which is not possible. There must be some bad logic in there somewhere.