Refactor histogram data construction