cleanup state handling, fix bugs with error state.