Bug fix: there can be infinite loop cases