Fix for flattening documents