refactor statistics functions