Refactor target node detection