better flattening, now for sorting