This is why we have a state monad...

...and doing it by hand is a little silly