update python script and fix a few small bugs