minor code update
fix C version
C version : speed improvments and fix memory leaks