Note derived from Martin Hedenfalk's btree.c; the code is significantly reworked but owes a lot to the original.