Avoid calling saveReferences recursivelly

This makes it possible to have circular references in documents without
creating an infinite loop when saving.
Test for the circular reference safe save
Better readability of intents for the last test
Coding Standard fixes
Merge pull request #15 from dripolles/master

Avoid calling saveReferences recursively