Basic statemachine construct and small fixes