working statemachine implementation