Moving duplicate checking earlier in the loop