Changes to reference counting part. Minor changes throughout.