Fixed the infinite loop from #137. The code still does not work, but now it does not work in a more reasonable way