Cleaned up some of the C code thanks to Mike