Full re-write.

The tree structure is simplified along with the logic that handles
normalization.
Sanitization is much better than before.