The code to walk through the list of discovered devices updates the index of the first device (sdev) when it should be updating the index of the currect device (tail).