Refactored Flattener