Switch index length check around to prevent crashes