Rewrite countChange function in recfun project