working c implementation