factorising code from pyper to py2r