replaced KeyError with IndexError