Why don't you return invalid paths instead? Wouldn't that make all this logic simpler?