Refactor index.toTree() to be clearer