Optimize cython code