Fixing some sorting problems