Fix for NSRangeExceptions in some cases