Updating connect method to prevent duplicate edges