Cherry-pick of lazycat's z-level kludge.