Change how affiliate structure is calculated and fix many bugs in affiliate structure system