Merged pull request by justinmc to handle cyclic references in deepCopy() function (with minor edits)