Add documentation about the states and transition conditions
Currently the code is very cryptic and it is very hard to review the behavior of the state machine.
The renaming of the variables helped a bit, but a proper document (or even a diagram) would be required