minor optimization concerning midpoint and sort