Re-work a loop to better use the stack of path elements, and fix error messaging.