Started fixing reference handling.
Reference handling now works in simple cases.