refactor Reference detection and resolv to be more modular