improved cython implementation