Changed dict.iteritems() to dict.items() for compatibility with python 3.x