Validate node nesting structure

This is a better version of #644. Fixes #101.