Cleanup.
Adapted to new backend.
Added initial well-formedness checking.