ensure return after data and/or status is written

move token creation and saving to common function