extracted logic from stream_metadata