Added code showing state logic