More tests, minor improvements to flattener code, minor refactoring.