Address PR comments and refactor codes.