Added a better check for index existence.