Fix bug: avoid infinite loop