More refactoring; added tree balancing based on AVL trees; not passing the large test yet