Optimize Cython code