moved r to outside of while in add_node