C code cleanup and speed improvements.