fixed sorting function