Use FSM and change algorithm.