State machine implementation