Use tern to sort ids by depth, remove scope info

This should reduce the number of times tern is called.