diff --git a/library/_cadbiom/cadbiom.c b/library/_cadbiom/cadbiom.c
index 99ac91543ac3a99345413094ae49274fcf53eac5..b19d5a32f404d0ee3d9f763aa09a22a7937ca86a 100644
--- a/library/_cadbiom/cadbiom.c
+++ b/library/_cadbiom/cadbiom.c
@@ -754,7 +754,11 @@ static PyObject* unflatten(PyObject *self, PyObject *args, PyObject *kwds)
                 Py_DECREF(item);
 
                 // Equivalent of get_unshift_code but in full C
-                unshift_code = Py_BuildValue("i", raw_get_unshift_code(&var_num, &shift_step)); // new ref
+                #ifdef IS_PY3K
+                unshift_code = PyLong_FromLong(raw_get_unshift_code(&var_num, &shift_step)); // new ref
+                #else
+                unshift_code = PyInt_FromLong(raw_get_unshift_code(&var_num, &shift_step)); // new ref
+                #endif
 
 
             if(unshift_code == NULL) {