fix KeyError and IndexError