added mutable state monad thing to be nice and safe