Statemachine implementation