finish implementation
optimization and code style fix