sort roots where necessary