Rewrite much of the code in C