Changed all loops over dictionaries d to call 'for item in d.keys()' instead of 'for item in d'. Should make it compatible with older python versions