Move the sorting to a better location