improved state machine handling, implemented line approach(unstable)