Fix segfault and clean up code

There was a segfault due to the GC collecting marked objects rather
than unmarked objects.