State Machine Optimization