simple finite-state machine implementation