refactor getNodeBefore method