Get rid of ugly state monad, replace with pure code