Handling circular references

I have fixed a bug causing the module to go into infinite loop if there are circular data structures.

Please consider my patches for inclusion.