A simple state machine implementation