Couple of changes:

Don't double-decrement the counter when walking back up the tree.
When looking for nodes, we should be checking for 'overlap' between two
ranges where one doens't contain the other. Old code was refusing
perfectly sensible things.
Against all reasonable expectation, working.