Fixed and cleaned up the state update.