fix infinite loop bug
add debug messages