fixing KeyError bug