Added break to outerloop when deciding whether the current state is the
final state.