Add text_state_dump.
Improve naming, and tidy up code.
improve maintenance.