Fixed sorting weirdness

Sort of. Still need to revisit, but this works for now.
One more change to tighten the sorting.

Still looks weird, still need to revisit.
Merge branch 'refs/heads/develop'