Fix SystemStackError

The test case included shows the bug, which is quite annoying.