Fixed how deleted objects are detected.

Some additional refactoring.