Optimized state machine