Starting to rewrite structure detection (it's now broken)