c rewrite

this is the pull request for the complete C rewrite