switch_state and clean up func