Split off the counting logic as per the suggestion of @CyberSecutor

@CyberSecutor