Fixed generation algorithm to properly remove dead-ends