Moving frequent computations to Cython for better performance.