fix some bugs and rewrite the core