Fix bug that caused sorting to fail