First attempt to c migration. Code cleaned up. Some comments added.