cleanup and sort output