Total rewrite to use a tree data structure instead of a flattened list.

Cleaner! Less special casing and pass throughs!