streamlined/improved state monad + tests