refactor code for python library, and added some new api.