Clear all data in database after receving all messages, add comments, improve code fomatting