Optimize the use of set() and small pep8 fix