fix a do/while crash and simplify the existing code
fix a do/while crash and simplify the existing code