MAINT: const correctness and minor fixes to C code