Simpler solution to avoid KeyError