Fixed transitions and made state machine a bit smarter.