diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp
index 1561f55f5..d4b7e8a92 100644
--- a/Telegram/SourceFiles/app.cpp
+++ b/Telegram/SourceFiles/app.cpp
@@ -1858,7 +1858,7 @@ namespace {
 		return ::histories;
 	}
 
-	History *history(const PeerId &peer) {
+	not_null<History*> history(const PeerId &peer) {
 		return ::histories.findOrInsert(peer);
 	}
 
diff --git a/Telegram/SourceFiles/app.h b/Telegram/SourceFiles/app.h
index fe267ed0c..531abac00 100644
--- a/Telegram/SourceFiles/app.h
+++ b/Telegram/SourceFiles/app.h
@@ -156,11 +156,11 @@ namespace App {
 	MTPPhoto photoFromUserPhoto(MTPint userId, MTPint date, const MTPUserProfilePhoto &photo);
 
 	Histories &histories();
-	History *history(const PeerId &peer);
+	not_null<History*> history(const PeerId &peer);
 	History *historyFromDialog(const PeerId &peer, int32 unreadCnt, int32 maxInboxRead, int32 maxOutboxRead);
 	History *historyLoaded(const PeerId &peer);
 	HistoryItem *histItemById(ChannelId channelId, MsgId itemId);
-	inline History *history(const PeerData *peer) {
+	inline not_null<History*> history(const PeerData *peer) {
 		Assert(peer != nullptr);
 		return history(peer->id);
 	}
diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp
index a4a17b30b..0867d6221 100644
--- a/Telegram/SourceFiles/history/history.cpp
+++ b/Telegram/SourceFiles/history/history.cpp
@@ -589,7 +589,7 @@ History *Histories::find(const PeerId &peerId) {
 	return (i == map.cend()) ? 0 : i.value();
 }
 
-History *Histories::findOrInsert(const PeerId &peerId) {
+not_null<History*> Histories::findOrInsert(const PeerId &peerId) {
 	auto i = map.constFind(peerId);
 	if (i == map.cend()) {
 		auto history = peerIsChannel(peerId) ? static_cast<History*>(new ChannelHistory(peerId)) : (new History(peerId));
@@ -598,7 +598,7 @@ History *Histories::findOrInsert(const PeerId &peerId) {
 	return i.value();
 }
 
-History *Histories::findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead) {
+not_null<History*> Histories::findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead) {
 	auto i = map.constFind(peerId);
 	if (i == map.cend()) {
 		auto history = peerIsChannel(peerId) ? static_cast<History*>(new ChannelHistory(peerId)) : (new History(peerId));
@@ -2099,6 +2099,21 @@ const ChannelHistory *History::asChannelHistory() const {
 	return isChannel() ? static_cast<const ChannelHistory*>(this) : 0;
 }
 
+not_null<History*> History::migrateToOrMe() const {
+	if (auto to = peer->migrateTo()) {
+		return App::history(to);
+	}
+	// We could get it by App::history(peer), but we optimize.
+	return const_cast<History*>(this);
+}
+
+History *History::migrateFrom() const {
+	if (auto from = peer->migrateFrom()) {
+		return App::history(from);
+	}
+	return nullptr;
+}
+
 bool History::isDisplayedEmpty() const {
 	return isEmpty() || ((blocks.size() == 1) && blocks.front()->items.size() == 1 && blocks.front()->items.front()->isEmpty());
 }
diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h
index 6ff1e1fea..6295562c0 100644
--- a/Telegram/SourceFiles/history/history.h
+++ b/Telegram/SourceFiles/history/history.h
@@ -53,8 +53,8 @@ public:
 	void step_typings(TimeMs ms, bool timer);
 
 	History *find(const PeerId &peerId);
-	History *findOrInsert(const PeerId &peerId);
-	History *findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead);
+	not_null<History*> findOrInsert(const PeerId &peerId);
+	not_null<History*> findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead);
 
 	void clear();
 	void remove(const PeerId &peer);
@@ -210,6 +210,9 @@ public:
 	ChannelHistory *asChannelHistory();
 	const ChannelHistory *asChannelHistory() const;
 
+	not_null<History*> migrateToOrMe() const;
+	History *migrateFrom() const;
+
 	bool isEmpty() const {
 		return blocks.isEmpty();
 	}
diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp
index bd06d725f..f83b174db 100644
--- a/Telegram/SourceFiles/history/history_inner_widget.cpp
+++ b/Telegram/SourceFiles/history/history_inner_widget.cpp
@@ -87,7 +87,7 @@ int BinarySearchBlocksOrItems(const T &list, int edge) {
 HistoryInner::HistoryInner(HistoryWidget *historyWidget, not_null<Window::Controller*> controller, Ui::ScrollArea *scroll, History *history) : TWidget(nullptr)
 , _controller(controller)
 , _peer(history->peer)
-, _migrated(history->peer->migrateFrom() ? App::history(history->peer->migrateFrom()->id) : nullptr)
+, _migrated(history->migrateFrom())
 , _history(history)
 , _widget(historyWidget)
 , _scroll(scroll)
@@ -2333,7 +2333,7 @@ void HistoryInner::notifyIsBotChanged() {
 }
 
 void HistoryInner::notifyMigrateUpdated() {
-	_migrated = _peer->migrateFrom() ? App::history(_peer->migrateFrom()->id) : 0;
+	_migrated = _history->migrateFrom();
 }
 
 int HistoryInner::moveScrollFollowingInlineKeyboard(const HistoryItem *item, int oldKeyboardTop, int newKeyboardTop) {
diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp
index b753fc021..f16395e8a 100644
--- a/Telegram/SourceFiles/history/history_widget.cpp
+++ b/Telegram/SourceFiles/history/history_widget.cpp
@@ -1526,7 +1526,7 @@ void HistoryWidget::notify_migrateUpdated(PeerData *peer) {
 			if (peer->migrateTo()) {
 				showHistory(peer->migrateTo()->id, (_showAtMsgId > 0) ? (-_showAtMsgId) : _showAtMsgId, true);
 			} else if ((_migrated ? _migrated->peer : 0) != peer->migrateFrom()) {
-				History *migrated = peer->migrateFrom() ? App::history(peer->migrateFrom()->id) : 0;
+				auto migrated = _history->migrateFrom();
 				if (_migrated || (migrated && migrated->unreadCount() > 0)) {
 					showHistory(peer->id, peer->migrateFrom() ? _showAtMsgId : ((_showAtMsgId < 0 && -_showAtMsgId < ServerMaxMsgId) ? ShowAtUnreadMsgId : _showAtMsgId), true);
 				} else {
@@ -1864,8 +1864,8 @@ void HistoryWidget::showHistory(const PeerId &peerId, MsgId showAtMsgId, bool re
 		_serviceImageCacheSize = imageCacheSize();
 		Auth().downloader().clearPriorities();
 
-		_history = App::history(_peer->id);
-		_migrated = _peer->migrateFrom() ? App::history(_peer->migrateFrom()->id) : 0;
+		_history = App::history(_peer);
+		_migrated = _history->migrateFrom();
 
 		if (_channel) {
 			updateNotifySettings();
diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp
index 076f3d2cd..d1f7a1f87 100644
--- a/Telegram/SourceFiles/mainwidget.cpp
+++ b/Telegram/SourceFiles/mainwidget.cpp
@@ -1645,7 +1645,7 @@ void MainWidget::checkLastUpdate(bool afterSleep) {
 	}
 }
 
-void MainWidget::overviewLoaded(History *history, const MTPmessages_Messages &result, mtpRequestId req) {
+void MainWidget::overviewLoaded(not_null<History*> history, const MTPmessages_Messages &result, mtpRequestId req) {
 	OverviewsPreload::iterator it;
 	MediaOverviewType type = OverviewCount;
 	for (int32 i = 0; i < OverviewCount; ++i) {
@@ -4009,7 +4009,10 @@ void MainWidget::onSelfParticipantUpdated(ChannelData *channel) {
 	auto history = App::historyLoaded(channel->id);
 	if (_updatedChannels.contains(channel)) {
 		_updatedChannels.remove(channel);
-		if ((history ? history : App::history(channel->id))->isEmpty()) {
+		if (!history) {
+			history = App::history(channel);
+		}
+		if (history->isEmpty()) {
 			checkPeerHistory(channel);
 		} else {
 			history->asChannelHistory()->checkJoinedMessage(true);
diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h
index e8578466e..af50b0f2e 100644
--- a/Telegram/SourceFiles/mainwidget.h
+++ b/Telegram/SourceFiles/mainwidget.h
@@ -515,7 +515,7 @@ private:
 	void readRequestDone(PeerData *peer);
 
 	void messagesAffected(PeerData *peer, const MTPmessages_AffectedMessages &result);
-	void overviewLoaded(History *history, const MTPmessages_Messages &result, mtpRequestId req);
+	void overviewLoaded(not_null<History*> history, const MTPmessages_Messages &result, mtpRequestId req);
 	void mediaOverviewUpdated(const Notify::PeerUpdate &update);
 
 	Window::SectionSlideParams prepareShowAnimation(bool willHaveTopBarShadow, bool willHaveTabbedSection);
diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp
index 92ec36807..0f0b4e7b7 100644
--- a/Telegram/SourceFiles/mainwindow.cpp
+++ b/Telegram/SourceFiles/mainwindow.cpp
@@ -267,7 +267,7 @@ void MainWindow::setupIntro() {
 
 void MainWindow::serviceNotification(const TextWithEntities &message, const MTPMessageMedia &media, int32 date, bool force) {
 	if (date <= 0) date = unixtime();
-	auto h = (_main && App::userLoaded(ServiceUserId)) ? App::history(ServiceUserId) : nullptr;
+	auto h = (_main && App::userLoaded(ServiceUserId)) ? App::history(ServiceUserId).get() : nullptr;
 	if (!h || (!force && h->isEmpty())) {
 		_delayedServiceMsgs.push_back(DelayedServiceMsg(message, media, date));
 		return sendServiceHistoryRequest();
diff --git a/Telegram/SourceFiles/media/player/media_player_instance.cpp b/Telegram/SourceFiles/media/player/media_player_instance.cpp
index 53e9857ee..501f89c5c 100644
--- a/Telegram/SourceFiles/media/player/media_player_instance.cpp
+++ b/Telegram/SourceFiles/media/player/media_player_instance.cpp
@@ -131,8 +131,8 @@ void Instance::setCurrent(const AudioMsgId &audioId) {
 			auto migrated = data->migrated;
 			auto item = data->current ? App::histItemById(data->current.contextId()) : nullptr;
 			if (item) {
-				data->history = item->history()->peer->migrateTo() ? App::history(item->history()->peer->migrateTo()) : item->history();
-				data->migrated = data->history->peer->migrateFrom() ? App::history(data->history->peer->migrateFrom()) : nullptr;
+				data->history = item->history()->migrateToOrMe();
+				data->migrated = data->history->migrateFrom();
 			} else {
 				data->history = nullptr;
 				data->migrated = nullptr;
diff --git a/Telegram/SourceFiles/overviewwidget.cpp b/Telegram/SourceFiles/overviewwidget.cpp
index 34c22e544..71f994483 100644
--- a/Telegram/SourceFiles/overviewwidget.cpp
+++ b/Telegram/SourceFiles/overviewwidget.cpp
@@ -55,8 +55,8 @@ OverviewInner::OverviewInner(OverviewWidget *overview, Ui::ScrollArea *scroll, P
 , _peer(peer->migrateTo() ? peer->migrateTo() : peer)
 , _type(type)
 , _reversed(_type != OverviewFiles && _type != OverviewLinks)
-, _migrated(_peer->migrateFrom() ? App::history(_peer->migrateFrom()->id) : 0)
-, _history(App::history(_peer->id))
+, _history(App::history(_peer))
+, _migrated(_history->migrateFrom())
 , _channel(peerToChannel(_peer->id))
 , _rowWidth(st::msgMinWidth)
 , _search(this, st::overviewFilter, langFactory(lng_dlg_filter))
diff --git a/Telegram/SourceFiles/overviewwidget.h b/Telegram/SourceFiles/overviewwidget.h
index bc0ad184b..adde68249 100644
--- a/Telegram/SourceFiles/overviewwidget.h
+++ b/Telegram/SourceFiles/overviewwidget.h
@@ -177,7 +177,8 @@ private:
 	PeerData *_peer;
 	MediaOverviewType _type;
 	bool _reversed;
-	History *_migrated, *_history;
+	History *_history;
+	History *_migrated;
 	ChannelId _channel;
 
 	bool _selMode = false;
diff --git a/Telegram/SourceFiles/profile/profile_block_shared_media.cpp b/Telegram/SourceFiles/profile/profile_block_shared_media.cpp
index 8841c5605..ba852b470 100644
--- a/Telegram/SourceFiles/profile/profile_block_shared_media.cpp
+++ b/Telegram/SourceFiles/profile/profile_block_shared_media.cpp
@@ -51,7 +51,7 @@ QString getButtonText(MediaOverviewType type, int count) {
 
 SharedMediaWidget::SharedMediaWidget(QWidget *parent, PeerData *peer) : BlockWidget(parent, peer, lang(lng_profile_shared_media))
 , _history(App::history(peer))
-, _migrated(peer->migrateFrom() ? App::history(peer->migrateFrom()) : nullptr) {
+, _migrated(_history->migrateFrom()) {
 	auto observeEvents = Notify::PeerUpdate::Flag::SharedMediaChanged
 		| Notify::PeerUpdate::Flag::UserCommonChatsChanged;
 	subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(observeEvents, [this](const Notify::PeerUpdate &update) {