Implement tree-based state system

Ref #10