added a much improved binning algorithm