changed method of checking for keys when handling errors