Can we reformulate this with the help of `evalWhile` in order to avoid duplication?