Better histogram calculation