Move state into a struct.

Perhaps this will simplify pointer arithmetic?