Refactor counting functions.