Fixed sorting behaviour (hopefully) plus some tuning