Refactor C code