worked out bugs in c implementation