c implementation in case python too slow