Check for key existence to avoid unhandled KeyError