add an import to work with newer cython, and other changes

Need to import malloc and free. (from libc.stdlib cimport malloc, free).
This also contains some other changes, mostly cython code to compute
character sums.