Improve flattener in preparation for garbage collection