StateMachine implementation using Scala