Fix recursive bug in handling history state changes