Attempt at faster and more full featured code

Now histograms will be built up from the 1d case. This approach improves
performance. It also adds a lot of functionality. Because of performance
requirements there is a little bit of duplicated code.