Reworking sorting layer helpers based on ChemiKhazi's pull request.