Changed if statement as it generated a KeyError