Fix bugs in ordering and groupping