make codes a little more readable
add a limit to stack to prevent stack overflow