Nit: it would be more consistent to return an error code here and pass a reference to the index as a parameter.