              :to           "me"})) (range n)))
 
 (defn store-message!
-  [db [_ {chat-id :from
+  [db [_ {chat-id  :from
           outgoing :outgoing
-          :as msg}]]
-  (let [previous-message (peek (get-in db [:chats chat-id :messages]))
-        msg (merge msg
-                   {:same-author     (if previous-message
-                                       (= (:from previous-message) outgoing)
-                                       true)
-                    :same-direction  (if previous-message
-                                       (= (:outgoing previous-message) outgoing)
-                                       true)})]
+          :as      msg}]]
+  (let [previous-message (first (get-in db [:chats chat-id :messages]))
+        msg              (merge msg
+                                {:same-author    (if previous-message
+                                                   (= (:from previous-message) outgoing)
+                                                   true)
+                                 :same-direction (if previous-message
+                                                   (= (:outgoing previous-message) outgoing)
+                                                   true)})]
     (save-message chat-id msg)))
+
+(defn add-message-to-db
+  [db chat-id {:keys [from outgoing] :as message}]
+  (let [messages         [:chats chat-id :messages]
+        previous-message (first (get-in db [:chats chat-id :messages]))
+        message          (merge message
+                                {:same-author    (if previous-message
+                                                   (= (:from previous-message) from)
+                                                   true)
+                                 :same-direction (if previous-message
+                                                   (= (:outgoing previous-message) outgoing)
+                                                   true)})]
+    (update-in db messages conj message)))
 
 (defn receive-message
   [db [_ {chat-id :from :as msg}]]
-  (let [messages [:chats chat-id :messages]]
-    (update-in db messages conj msg)))
+  (add-message-to-db db chat-id msg))
 
 (register-handler :received-msg
   (-> receive-message

(defn add-message
  [{:keys [new-message current-chat-id] :as db}]
  (if new-message
    (update-in db [:chats current-chat-id :messages] conj new-message)
    db))

(defn add-commands
  [{:keys [new-commands current-chat-id] :as db}]
  (reduce
    #(update-in %1 [:chats current-chat-id :messages] conj %2)
    db
    new-commands))



(defn prepare-message
  [{:keys [identity current-chat-id] :as db} _]
  (let [text (get-in db [:chats current-chat-id :input-text])
        {:keys [command]} (check-suggestion db (str text " "))]
    (if command
      (set-chat-command db command)
