Refactored reference resolving code