Fixes logic errors in the art lookup code which can result in segfaults or lookup loops.
Based in part on !94 (closed).