fix some bugs. Now it can run correctly. I'll add some comments to the source code later.