refine code and fix some bug

Refine code and fix some bug