Merge pull request #6 from doriath/fix-infinite-loop-bug

Fix a bug that causes infinite loop.