some improvements

inlined defers
made it thread-safer
made it nil safer (nil represents an empty set)
... down to line 214, there I stopped...