added timeout and improved circular reference detection