--- Core.xs.PL_orig	Wed Aug 17 17:48:02 2011
+++ Core.xs.PL	Thu Aug 18 12:39:56 2011
@@ -370,8 +370,9 @@
 void
 DESTROY(sv)
   SV *	sv;
-  CODE:
+  PREINIT:
     pdl *self;
+  CODE:
     if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV)
        1; /* Do nothing */
     else {
@@ -765,12 +766,12 @@
 SV *
 sclr_c(it)
    pdl* it
-   CODE:
+   PREINIT:
 	PDL_Long nullp = 0;
 	PDL_Long dummyd = 1;
 	PDL_Long dummyi = 1;
 	double result;
-
+   CODE:
         /* get the first element of a piddle and return as
          * Perl double scalar (NV)
          */
@@ -790,14 +791,16 @@
 SV *
 at_c(x,position)
    pdl*	x
-   PDL_Long *	pos = NO_INIT
-   CODE:
-    int npos, ipos;
+   SV*	position
+   PREINIT:
+    PDL_Long * pos;
+    int npos;
+    int ipos;
     double result;
+   CODE:
+    pdl_make_physvaffine( x );
 
-      pdl_make_physvaffine( x );
-
-    pos = pdl_packdims( ST(1), &npos);
+    pos = pdl_packdims( position, &npos);
     
     if (pos == NULL || npos < x->ndims)
        croak("Invalid position");
@@ -823,14 +826,17 @@
 SV *
 at_bad_c(x,position)
    pdl*	x
-   PDL_Long *	pos = NO_INIT
-   CODE:
-    int npos, ipos, badflag;
+   SV *	position
+   PREINIT:
+    PDL_Long * pos;
+    int npos;
+    int ipos;
+    int badflag;
     double result;
+   CODE:
+    pdl_make_physvaffine( x );
 
-      pdl_make_physvaffine( x );
-
-    pos = pdl_packdims( ST(1), &npos);
+    pos = pdl_packdims( position, &npos);
     
     if (pos == NULL || npos < x->ndims)
        croak("Invalid position");
@@ -891,12 +897,15 @@
 void
 list_c(x)
 	pdl *x
-	PPCODE:
-	PDL_Long *inds,*incs,offs;
+      PREINIT:
+	PDL_Long *inds;
+      PDL_Long *incs;
+      PDL_Long offs;
 	void *data;
 	int ind;
 	int stop = 0;
-        pdl_make_physvaffine( x );
+	PPCODE:
+      pdl_make_physvaffine( x );
 	inds = pdl_malloc(sizeof(PDL_Long) * x->ndims); /* GCC -> on stack :( */
 
 	data = PDL_REPRP(x);
@@ -921,12 +930,16 @@
 SV *
 listref_c(x)
    pdl *x
-  CODE:
-   PDL_Long *inds,*incs,offs;
+  PREINIT:
+   PDL_Long * inds;
+   PDL_Long * incs;
+   PDL_Long * offs;
    void *data;
-   int ind, lind;
+   int ind;
+   int lind;
    int stop = 0;
    AV *av;
+  CODE:
 
 !NO!SUBS!
 
@@ -1027,14 +1040,16 @@
 void
 set_c(x,position,value)
     pdl*	x
-    PDL_Long *	pos = NO_INIT
+    SV*	position
     double	value
+   PREINIT:
+    PDL_Long * pos;
+    int npos;
+    int ipos;
    CODE:
-    int npos,ipos;
-
     pdl_make_physvaffine( x );
 
-    pos = pdl_packdims( ST(1), &npos);
+    pos = pdl_packdims( position, &npos);
     if (pos == NULL || npos < x->ndims)
        croak("Invalid position");
 
@@ -1182,14 +1197,14 @@
      SV* array_ref
      char* class
      int type
-  CODE:
-     /* make a piddle from a Perl array ref */
+  PREINIT:
      AV *dims, *av;
      int i, depth;
      int datalevel = -1;
      SV* psv;
      pdl* p;
-
+  CODE:
+     /* make a piddle from a Perl array ref */
      if (!SvROK(array_ref))
        croak("pdl_avref: not a reference");
      if (SvTYPE(SvRV(array_ref)) != SVt_PVAV)
@@ -1298,10 +1313,9 @@
 SV *
 initialize(class)
 	SV *class
-
-        PPCODE:
+        PREINIT:
 	HV *bless_stash;
-
+        PPCODE:
         if (SvROK(class)) { /* a reference to a class */
 	  bless_stash = SvSTASH(SvRV(class));
         } else {            /* a class name */
@@ -1335,8 +1349,9 @@
 int
 upd_data(self)
 	pdl *self
-	CODE:
+      PREINIT:
        STRLEN n_a;
+	CODE:
 	if(self->state & PDL_DONTTOUCHDATA) {
 		croak("Trying to touch dataref of magical (mmaped?) pdl");
 	}
@@ -1410,14 +1425,17 @@
 		RETVAL
 
 void
-setdims(x,dims)
+setdims(x,dims_arg)
 	pdl *x
-	PDL_Long *dims = NO_INIT
+      SV * dims_arg
+      PREINIT:
+	 PDL_Long * dims;
+	 int ndims;
+       int i;
 	CODE:
 	{
-		int ndims; int i;
 		pdl_children_changesoon(x,PDL_PARENTDIMSCHANGED|PDL_PARENTDATACHANGED);
-		dims = pdl_packdims(ST(1),&ndims);
+		dims = pdl_packdims(dims_arg,&ndims);
 		pdl_reallocdims(x,ndims);
 		for(i=0; i<ndims; i++) x->dims[i] = dims[i];
 		pdl_resize_defaultincs(x);
@@ -1454,8 +1472,9 @@
 sethdr(p,h)
 	pdl *p
 	SV *h
-	CODE:
+      PREINIT:
 	HV* hash;
+	CODE:
 		if(p->hdrsv == NULL) {             
 		      p->hdrsv =  &PL_sv_undef; /*(void*) newSViv(0);*/
 		} 
@@ -1526,9 +1545,11 @@
 
 void
 threadover_n(...)
+   PREINIT:
+   int npdls;
    CODE:
    {
-    int npdls = items - 1;
+    npdls = items - 1;
     if(npdls <= 0)
     	croak("Usage: threadover_n(pdl[,pdl...],sub)");
     {
@@ -1567,10 +1588,13 @@
 
 void
 threadover(...)
+   PREINIT:
+    int npdls;
+    int targs;
+    int nothers = -1;
    CODE:
    {
-    int npdls, nothers = -1;
-    int targs = items - 4;
+        targs = items - 4;
     if (items > 0) nothers = SvIV(ST(0));
     if(targs <= 0 || nothers < 0 || nothers >= targs)
     	croak("Usage: threadover(nothers,pdl[,pdl...][,otherpars..],realdims,creating,sub)");
