devilish tricks to turn a flat list into a tree