refactored entropy counting