Fix bug causing an infinite loop