Refine name resolution to be closer to Python's