Fix and sort history