Fix for unsorted input

Output was incorrect when input bin labels were not sorted. Fixed now.