State machine working