Fixing the sorting problem