A statemachine implementation