refractor, fix a few bug