added cyclic reference detection, fixed pointer and reference
interaction to pass the reference encoding tests