split the working code out from the XS file