implement optimization with a state monad
.