I'm not sure I understand this part of the code.
You seem to change the affinity type: was this only to have the code run without failing ?