some progress porting C code to Python