Refactor codes and docs
Fix a bug