Please move lines 63-75 outside the b-tag weight loop. They don't need to have them applied as the sum over all is 1 anyway.