Automatic binning

Supersedes #258