Consolidate some of the flattening code

Signed-off-by: Rick Taylor <rick@tropicalstormsoftware.com>
Implicit negation of <

Signed-off-by: Rick Taylor <rick@tropicalstormsoftware.com>
Improved error handling

Signed-off-by: Rick Taylor <rick@tropicalstormsoftware.com>
Hugely improved conditional and block reduction

Signed-off-by: Rick Taylor <rick@tropicalstormsoftware.com>