use histogram for major speed-up