optimizing number of stages