Lei : add debug parameter and refactor code