Fixed detection of transitively orphan packages

Closes #1831, #1834