State machine for D