check for valid index