Refactored histogram code