Fixing stack overflow