Moving some of the AVL tree code into more generic places