Refactored addNextNode and addNodeToHeader.