refactor the code
add debug code