improved logic for nodes with multiple edges