Changed code so that each node contains a reference to its parent