Use more clever algorithm for automatical choice of histogram range

Fixes #57