Index: libpolys/coeffs/longrat.cc
===================================================================
--- libpolys/coeffs/longrat.cc	(revision fc497757365979d0168460de9969b49bc49c53f2)
+++ libpolys/coeffs/longrat.cc	(revision 146c6031e29bacb7405aa0a52f67591c3cfd1d1f)
@@ -1268,9 +1268,30 @@
   {
     int in=mpz_fdiv_ui(n->n,(unsigned long)p);
-    #ifdef NV_OPS
-    if (p>NV_MAX_PRIME)
-    return (int)((long)nvDiv((number)iz,(number)in,(const coeffs)r));
-    #endif
-    return (int)((long)npDiv((number)iz,(number)in,(const coeffs)r));
+    long  s, t;
+
+    long  u, v, u0, v0, u1, v1, u2, v2, q, r;
+
+     u1=1; v1=0;
+     u2=0; v2=1;
+     u = in; v = p;
+
+     while (v != 0)
+     {
+        q = u / v;
+        r = u % v;
+        u = v;
+        v = r;
+        u0 = u2;
+        v0 = v2;
+        u2 =  u1 - q*u2;
+        v2 = v1- q*v2;
+        u1 = u0;
+        v1 = v0;
+     }
+
+     s = u1;
+     if (s < 0) s+=p;
+     u=(s*((long)iz)) % ((long)p);
+     return (int)u;
   }
   return iz;
Index: libpolys/polys/clapconv.cc
===================================================================
--- libpolys/polys/clapconv.cc	(revision fc497757365979d0168460de9969b49bc49c53f2)
+++ libpolys/polys/clapconv.cc	(revision 146c6031e29bacb7405aa0a52f67591c3cfd1d1f)
@@ -26,8 +26,4 @@
 
 static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
-
-static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs, const ring r );
-
-static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result );
 
 static number convFactoryNSingAN( const CanonicalForm &f, const ring r);
@@ -105,25 +101,19 @@
 }
 
-int convFactoryISingI( const CanonicalForm & f)
-{
-  if (!f.isImm()) WerrorS("int overflow in det");
-  return f.intval();
-}
-#if 0
-CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
+CanonicalForm convSingAFactoryA( number pp, Variable a, const coeffs cf )
 {
   CanonicalForm result = 0;
-  int e, n = pVariables;
-  int off=rPar(currRing);
+  int e;
+  poly p=(poly)pp;
+  BOOLEAN setChar=TRUE;
 
-  On(SW_RATIONAL);
-  while ( p!=NULL)
+  while ( p!=NULL )
   {
-    CanonicalForm term=convSingAFactoryA(((lnumber)pGetCoeff(p))->z,a, r);
-    for ( int i = 1; i <= n; i++ )
-    {
-      if ( (e = pGetExp( p, i )) != 0 )
-        term *= power( Variable( i + off), e );
-    }
+    CanonicalForm term;
+    term=cf->extRing->cf->convSingNFactoryN(pGetCoeff( p ),setChar, cf->extRing->cf);
+    if (errorreported) break;
+    setChar=FALSE;
+    if ( (e = p_GetExp( p, 1, cf->extRing)) != 0 )
+        term *= power( a, e );
     result += term;
     pIter( p );
@@ -132,140 +122,5 @@
 }
 
-static void
-convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
-
-poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
-{
-  int n = rVar(r)+rPar(r)+1;
-  int * exp = (int *)omAlloc0(n*sizeof(int));
-  poly result = NULL;
-  convRecAP_R( f, exp, result,par_start, var_start, r );
-  omFreeSize((ADDRESS)exp,n*sizeof(int));
-  return result;
-}
-
-poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
-{
-  return convFactoryAPSingAP_R(f,0,rPar(r),r);
-}
-
-static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
-{
-  if (f.isZero())
-    return;
-  if ( ! f.inCoeffDomain() )
-  {
-    int l = f.level();
-    for ( CFIterator i = f; i.hasTerms(); i++ )
-    {
-      exp[l] = i.exp();
-      convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
-    }
-    exp[l] = 0;
-  }
-  else
-  {
-    poly z=(poly)convFactoryASingA( f,r );
-    if (z!=NULL)
-    {
-      poly term = p_Init(r);
-      pNext( term ) = NULL;
-      int i;
-      for ( i = rVar(r); i>0 ; i-- )
-        p_SetExp( term, i , exp[i+var_start],r);
-      //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
-      if (par_start==0)
-      {
-        for ( i = 1; i <= var_start; i++ )
-        //z->e[i-1]+=exp[i];
-          p_AddExp(z,i,exp[i],r->extRing);
-      }
-      else
-      {
-        for ( i = par_start+1; i <= var_start+rPar(currRing); i++ )
-        //z->e[i-1]+=exp[i];
-          p_AddExp(z,i,exp[i-par_start],r->extRing);
-      }
-      pGetCoeff(term)=(number)ALLOC0_LNUMBER();
-      ((lnumber)pGetCoeff(term))->z=z;
-      p_Setm( term,r );
-      result = p_Add_q( result, term, r );
-    }
-  }
-}
-
-CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
-{
-  CanonicalForm result = 0;
-  int e;
-
-  while ( p!=NULL )
-  {
-    CanonicalForm term;
-    /* does only work for finite fields:Z/p */
-    if ( rField_is_Zp_a() )
-    {
-      term = npInt( napGetCoeff( p ),r );
-    }
-    else
-    {
-      if ( SR_HDL(napGetCoeff( p )) & SR_INT )
-        term = SR_TO_INT(napGetCoeff( p )) ;
-      else
-      {
-        if ( napGetCoeff( p )->s == 3 )
-        {
-          lint dummy;
-          mpz_init_set( &dummy, (napGetCoeff( p )->z) );
-          term = make_cf( dummy );
-        }
-        else
-        {
-          // assume s==0 or s==1
-          lint num, den;
-          On(SW_RATIONAL);
-          mpz_init_set( &num, (napGetCoeff( p )->z) );
-          mpz_init_set( &den, (napGetCoeff( p )->n) );
-          term = make_cf( num, den, ( napGetCoeff( p )->s != 1 ));
-        }
-      }
-    }
-    if ( (e = p_GetExp( p, 1, r->extRing )) != 0 )
-      term *= power( a , e );
-    result += term;
-    p = pNext( p );
-  }
-  return result;
-}
-
-static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
-{
-  if ( f.isImm() )
-    return n_Init( f.intval(), r->extRing );
-  else
-  {
-    number z=ALLOC_RNUMBER();
-#if defined(LDEBUG)
-    z->debug=123456;
-#endif
-    gmp_numerator( f, z->z );
-    if ( f.den().isOne() )
-    {
-      z->s = 3;
-    }
-    else
-    {
-      gmp_denominator( f, z->n );
-      z->s = 0;
-      nlNormalize(z);
-    }
-    #ifdef LDEBUG
-    nlDBTest(z,__FILE__,__LINE__);
-    #endif
-    return z;
-  }
-}
-
-poly convFactoryASingA ( const CanonicalForm & f, const ring r )
+number convFactoryASingA ( const CanonicalForm & f, const coeffs cf )
 {
   poly a=NULL;
@@ -273,192 +128,27 @@
   for( CFIterator i=f; i.hasTerms(); i++)
   {
-    t=napNew();
+    t=p_New(cf->extRing);
     // pNext( t ) = NULL; //already done by napNew
-    pGetCoeff(t)=convFactoryNSingAN( i.coeff(), r );
-    if (n_IsZero(napGetCoeff(t),r->extRing))
+    pGetCoeff(t)=cf->extRing->cf->convFactoryNSingN( i.coeff(), cf->extRing->cf );
+    if (n_IsZero(p_GetCoeff(t,cf->extRing),cf->extRing->cf))
     {
-      p_Delete(&t,r->extRing);
+      p_Delete(&t,cf->extRing);
     }
     else
     {
-      p_SetExp(t,1,i.exp(),r->extRing);
-      a=p_Add_q(a,t,r->extRing);
+      p_SetExp(t,1,i.exp(),cf->extRing);
+      a=p_Add_q(a,t,cf->extRing);
     }
   }
-  if (a!=NULL)
-  {
-    if (r->minpoly!=NULL)
-    {
-      lnumber l=(lnumber)r->minpoly;
-      if (p_GetExp(a,1,r->extRing) >= p_GetExp(l->z,1,r->extRing))
-        a = napRemainder( a, l->z);
-    }
-  }
-  return a;
+  number aa=(number)a;
+  n_Normalize(aa,cf);
+  return aa;
 }
 
-CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
+
+int convFactoryISingI( const CanonicalForm & f)
 {
-  CanonicalForm result = 0;
-  int e, n = rVar(r);
-  int offs = rPar(r);
-
-  while ( p!=NULL )
-  {
-    n_Normalize(pGetCoeff(p),r);
-    CanonicalForm term=convSingPFactoryP(((lnumber)pGetCoeff(p))->z,r->extRing);
-
-    if ((((lnumber)pGetCoeff(p))->n!=NULL)
-    && (!errorreported))
-    {
-      WerrorS("conversion error: denominator!= 1");
-    }
-
-    for ( int i = n; i > 0; i-- )
-    {
-      if ( (e = p_GetExp( p, i,r )) != 0 )
-        term = term * power( Variable( i + offs ), e );
-    }
-    result += term;
-    p = pNext( p );
-  }
-  return result;
+  if (!f.isImm()) WerrorS("int overflow in det");
+  return f.intval();
 }
-
-poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
-{
-  int n = rVar(r)+1;
-  int * exp = (int*)omAlloc0(n*sizeof(int));
-  poly result = NULL;
-  convRecTrP( f, exp, result , rPar(r), r );
-  omFreeSize((ADDRESS)exp,n*sizeof(int));
-  return result;
-}
-
-static void
-convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
-{
-  if (f.isZero())
-    return;
-  if ( f.level() > offs )
-  {
-    int l = f.level();
-    for ( CFIterator i = f; i.hasTerms(); i++ )
-    {
-      exp[l-offs] = i.exp();
-      convRecTrP( i.coeff(), exp, result, offs, r );
-    }
-    exp[l-offs] = 0;
-  }
-  else
-  {
-    poly term = p_Init(r);
-    pNext( term ) = NULL;
-    for ( int i = rVar(r); i>0; i-- )
-      p_SetExp( term, i ,exp[i], r);
-    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
-    pGetCoeff(term) = ALLOC0_RNUMBER(); // Q!?
-    ((lnumber)pGetCoeff(term))->z=convFactoryPSingP( f, r->extRing );
-    p_Setm( term,r );
-    result = p_Add_q( result, term,r );
-  }
-}
-
-number   nlChineseRemainder(number *x, number *q,int rl)
-// elemenst in the array are x[0..(rl-1)], q[0..(rl-1)]
-{
-#ifdef HAVE_FACTORY
-  setCharacteristic( 0 ); // only in char 0
-  CFArray X(rl), Q(rl);
-  int i;
-  for(i=rl-1;i>=0;i--)
-  {
-    X[i]=convSingNFactoryN(x[i],NULL); // may be larger MAX_INT
-    Q[i]=convSingNFactoryN(q[i],NULL); // may be larger MAX_INT
-  }
-  CanonicalForm xnew,qnew;
-  chineseRemainder(X,Q,xnew,qnew);
-  number n=convFactoryNSingN(xnew,NULL);
-  number p=convFactoryNSingN(qnew,NULL);
-  number p2=nlIntDiv(p,nlInit(2, NULL));
-  if (nlGreater(n,p2))
-  {
-     number n2=nlSub(n,p);
-     nlDelete(&n,NULL);
-     n=n2;
-  }
-  nlDelete(&p,NULL);
-  nlDelete(&p2,NULL);
-  return n;
-#else
-  WerrorS("not implemented");
-  return nlInit(0);
 #endif
-}
-
-CanonicalForm
-convSingGFFactoryGF( poly p )
-{
-  CanonicalForm result=CanonicalForm(0);
-  int e, n = pVariables;
-
-  while ( p != NULL )
-  {
-    CanonicalForm term;
-    term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
-    //int * A=(int *)&term;
-    //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
-    for ( int i = 1; i <= n; i++ )
-    {
-      if ( (e = pGetExp( p, i )) != 0 )
-         term *= power( Variable( i ), e );
-    }
-    result += term;
-    p = pNext( p );
-  }
-  return result;
-}
-
-poly
-convFactoryGFSingGF ( const CanonicalForm & f )
-{
-//    cerr << " f = " << f << endl;
-  int n = pVariables+1;
-  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
-  int * exp = (int*)omAlloc0(n*sizeof(int));
-  poly result = NULL;
-  convRecGFGF( f, exp, result );
-  omFreeSize((ADDRESS)exp,n*sizeof(int));
-  return result;
-}
-
-static void
-convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
-{
-  if (f.isZero())
-    return;
-  if ( ! f.inCoeffDomain() )
-  {
-    int l = f.level();
-    for ( CFIterator i = f; i.hasTerms(); i++ )
-    {
-      exp[l] = i.exp();
-      convRecGFGF( i.coeff(), exp, result );
-    }
-    exp[l] = 0;
-  }
-  else
-  {
-    poly term = pInit();
-    pNext( term ) = NULL;
-    for ( int i = 1; i <= pVariables; i++ )
-      pSetExp( term, i, exp[i]);
-    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
-    pGetCoeff( term ) = (number) gf_value (f);
-    pSetm( term );
-    result = pAdd( result, term );
-  }
-}
-
-#endif
-#endif
Index: libpolys/polys/clapconv.h
===================================================================
--- libpolys/polys/clapconv.h	(revision fc497757365979d0168460de9969b49bc49c53f2)
+++ libpolys/polys/clapconv.h	(revision 146c6031e29bacb7405aa0a52f67591c3cfd1d1f)
@@ -15,5 +15,4 @@
 
 #include <polys/monomials/ring.h>
-//#include <polys/ext_fields/longtrans.h>
 #include <factory/factory.h>
 
@@ -21,19 +20,8 @@
 poly convFactoryPSingP ( const CanonicalForm & f, const ring r );
 CanonicalForm convSingPFactoryP( poly p, const ring r );
-//CanonicalForm convSingIFactoryI( int i);// by constructor of CanonicalForm
 int convFactoryISingI( const CanonicalForm & f);
 
-//CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r );
-//poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r );
-//poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start );
-
-//CanonicalForm convSingGFFactoryGF ( poly p, const ring r );
-//poly convFactoryGFSingGF ( const CanonicalForm & f, const ring r );
-
-//CanonicalForm convSingAFactoryA ( number p , const Variable & a, const ring r );
-//number convFactoryASingA ( const CanonicalForm & f, const ring r );
-
-//CanonicalForm convSingTrPFactoryP ( poly p, const ring r );
-//poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r );
+CanonicalForm convSingAFactoryA( number pp, Variable a, const coeffs cf );
+number convFactoryASingA ( const CanonicalForm & f, const coeffs cf );
 
 // HAVE_FACTORY
Index: libpolys/polys/ext_fields/algext.cc
===================================================================
--- libpolys/polys/ext_fields/algext.cc	(revision fc497757365979d0168460de9969b49bc49c53f2)
+++ libpolys/polys/ext_fields/algext.cc	(revision 146c6031e29bacb7405aa0a52f67591c3cfd1d1f)
@@ -44,4 +44,9 @@
 #include <polys/simpleideals.h>
 
+#ifdef HAVE_FACTORY
+#include <polys/clapconv.h>
+#include <factory/factory.h>
+#endif
+
 #include "ext_fields/algext.h"
 #define TRANSEXT_PRIVATES 1
@@ -522,4 +527,28 @@
 }
 
+void  naNormalize(number &a, const coeffs cf)
+{
+  poly aa=(poly)a;
+  definiteReduce(aa,naMinpoly,cf); 
+  a=(number)aa;
+}
+
+#ifdef HAVE_FACTORY
+number naConvFactoryNSingN( const CanonicalForm n, const coeffs cf)
+{
+  if (n.isZero()) return NULL;
+  poly p=convFactoryPSingP(n,naRing);
+  return (number)p;
+}
+CanonicalForm naConvSingNFactoryN( number n, BOOLEAN setChar, const coeffs cf )
+{
+  naTest(n);
+  if (n==NULL) return CanonicalForm(0);
+
+  return convSingPFactoryP((poly)n,naRing);
+}
+#endif
+
+
 /* IMPORTANT NOTE: Since an algebraic field extension is again a field,
                    the gcd of two elements is not very interesting. (It
@@ -735,4 +764,5 @@
   cf->cfImPart       = naImPart;
   cf->cfCoeffWrite   = naCoeffWrite;
+  cf->cfNormalize    = naNormalize;
 #ifdef LDEBUG
   cf->cfDBTest       = naDBTest;
@@ -744,4 +774,8 @@
   cf->cfInvers       = naInvers;
   cf->cfIntDiv       = naDiv;
+#ifdef HAVE_FACTORY
+  cf->convFactoryNSingN=naConvFactoryNSingN;
+  cf->convSingNFactoryN=naConvSingNFactoryN;
+#endif
   
   return FALSE;
