C version almost done