Fix infinite loop

Closes #1.