More restructuring and rewriting of the C code.