I couldn’t read your code because it wasn’t formatted correctly. Please indent properly and then surround the code with triple-backquotes. Code Formatting
I have edited your post.
I don’t see anything odd about your code, so I cannot explain why you are getting odd behavior when you add a node. Perhaps you could explain how you do that?