Add state checking to avoid inconsistent state