Optimization of multistage processes with product recycle