properly flatten considering links, though this needs more testing