merge function GetPathElementEnd and GetPathElement to one function