Instead of relying on varibales to find parents and grandparents, I now recalculate them each time - to be absolutely sure I will get the right ones. As it stands it seems inserting nodes works as it should