Changed approach to use local state machines instead of global