Corrected to allow unsorted outputs.