Fix stack limit exceeded

This fixes the stack limit exceeded problem discusses in merge request 147