Rewritten find-edges function.