Possible fix for KeyError problem