Merging and flattening layers