Refactoring of statemachine