Fix max call stack error/bug

This is the fix for http://manage.dimagi.com/default.asp?133655