Correct looping for the last node