Term simplification and bug fix

Removed the silly intermediate terms for each multiply

Computation of gain was in wrong order. Worked only because
one of the arrays was 1x1 so it didn't matter.