Fix bugs in python code