Correctly number new nodes

Previously, the new node number was inferred from counting the
number of nodes of that kind. However, this would conflict if a
earlier node had already been deleted.

The number of the new node is now inferred from the highest-numbered
node of the kind instead, which should avoid these issues.