added stack-based state machine