Eliminate possible mem leaks
Add conditionals arround obj ref/unref