Some language fixes. Introduced a state machine.