fixed KeyError, added try/except block