Refactoring of statemachine
partly gets the new statemachine working...