Implemented a statehandler thread