Optimize cython

Found a few places where the Cython code can be optimized. A few of the functions in ``core.pyx`` can still be optimized by making them into C functions (``cdef``) but they then need to return C objects, not tuples of variables.