Add flatten and tests