Moved logic from encoder to CoefficientArithmetic