Just noticed this:  Once you exit this loop on i==0, you'll never re-enter on a different i.  id1 and id2 will still have the same values on the next pass, id1 != id2, so you'll never get new values for the IDs.