More fixes to state machine code and direction handling