Made use of cached distance images more robust