move some code from python to cython