Fixes and improvements

It looks like quite a big change, but if you look at it on a per changeset basis, it makes more sense.

The first changeset was fixing non-existing nodes appearing in the node list, but doesn't actually fix the issue properly, so this is a second attempt.

Second is another issue that exists.

Third is just an optimization.