Fixed getLastNode method