save recursively through references if necessary