accum rewritten with writer monad