added KeyError exception handling