refactored hamt code, untested