Check for the existence of a node before inserting a new one

This PR fixes an potential issue.