add remapping device selection logic.

also fixesa  numbero f bugs in the tree building code.