--- a/Classes/Field/Deck.cs
+++ b/Classes/Field/Deck.cs
@@ -1,15 +1,15 @@
 ďťżusing System;
+using System.Collections;
 using System.Collections.Generic;
-using System.Linq;
-using System.Text;
 using MySql.Data.MySqlClient;
 
 namespace YuGiOh_Ultra {
-    public sealed class Deck : IEnumerable<Card> {
-        private readonly uint _id;
+    public sealed class Deck : IEnumerable<ICard> {
+        public readonly uint Id;
 
         public string Name { get; private set; }
-        private readonly CardStack _realcardsstructure; //the real deck structure on database
+        //the real deck structure on database
+        private readonly Dictionary<ICard, uint> _realcardsstructure = new Dictionary<ICard, uint>();
         public CardStack Cards; //deck in game
         public uint Count { get { return Cards.Count; } }
 
@@ -18,59 +18,74 @@
         /// </summary>
         /// <param name="deckid">the id of the deck</param>
         public Deck( uint deckid ) { //load realcards structure!
-            this._id = deckid;
-            _realcardsstructure = new CardStack();
+            this.Id = deckid;
 
             var con = new MySqlConnection( Com.connStr );
             con.Open();
-            var cmd = new MySqlCommand( "SELECT `name`, `cards` FROM `deck` WHERE `id` =" + _id + " LIMIT 1;", con );
+            var cmd = new MySqlCommand( "SELECT `name` FROM `deck` WHERE `id` =" + Id + " LIMIT 1;", con );
             var reader = cmd.ExecuteReader();
 
             if( !reader.Read() ) throw new Exception("Can't load Deck!");
             Name = reader.GetString( "name" );
-            if( reader.GetString( "cards" ).Length > 0 ) {
-                var arytext = reader.GetString( "cards" ).Split( '|' );
 
-                for( int i = 0; i < arytext.Length; i++ ) {
-                    var cc = new MySqlConnection( Com.connStr );
-                    cc.Open();
-                    var cmcinfo = new MySqlCommand( "SELECT * FROM cards WHERE `code` = '" + arytext[i].ToUpper() + "' LIMIT 1", cc );
-                    var rdcinfo = cmcinfo.ExecuteReader();
-                    if( rdcinfo.Read() ) {
-                        var c = Card.Load( rdcinfo );
-                        if( c != null ) _realcardsstructure.AddTop( c );
-                    }
-                       
-                    rdcinfo.Close();
-                    cmcinfo.Dispose();
-                    cc.Close();
+            {
+                var cc = new MySqlConnection( Com.connStr );
+                cc.Open();
+                var deckcards =
+                    new MySqlCommand(
+                        "SELECT cards.*, `quantity` FROM `deck_cards` JOIN cards ON ( card = code ) WHERE `deckid` =" +
+                        Id + " AND `location` = 'd'", cc );
+                var readcards = deckcards.ExecuteReader();
+                while ( readcards.Read() ) {
+                    var c = ACard.Load( readcards );
+                    if ( c != null ) _realcardsstructure.Add( c, readcards.GetUInt16( "quantity" ) );
+                    else throw new InvalidCardException();
                 }
-                Cards = new CardStack( _realcardsstructure );
-                this.Shuffle();
-            } else Cards = new CardStack();
+
+                deckcards.Dispose();
+                readcards.Close();
+                cc.Close();
+            }
+            Reset();
 
             reader.Close();
             cmd.Dispose();
             con.Close();
         }
 
-        public void AddToStructure( Card c ) { _realcardsstructure.AddTop( c ); }
+        public void AddToStructure( ICard c ) {
+            if( c == null ) throw new InvalidCardException();
+            if ( _realcardsstructure.ContainsKey( c ) ) _realcardsstructure[c]++;
+            else _realcardsstructure.Add( c, 1 );
+        }
 
-        public void RemoveFormStructure( Card c ) { _realcardsstructure.Remove( c ); }
+        /// <summary>
+        /// Remove 1 occurence of the card
+        /// </summary>
+        /// <param name="c">The card</param>
+        public void RemoveFromStructure( ICard c ) {
+            if ( !_realcardsstructure.ContainsKey( c ) ) return;
+            _realcardsstructure[c]--;
+
+            if ( _realcardsstructure[c] == 0 ) _realcardsstructure.Remove( c );
+        }
 
         /// <summary>
         /// draws 1 card
         /// </summary>
-        /// <returns>returns the drew card</returns>
-        public Card Draw() { return Cards.Remove(); }
+        /// <returns>returns the drew card, if deck is empity returns null</returns>
+        public ICard Draw() {
+            if ( Cards.IsEmpity() ) throw new EmptyStackException();
+            return Cards.Remove();
+        }
 
         /// <summary>
         /// draws num cards
         /// </summary>
         /// <param name="num">number of cards</param>
         /// <returns>returns a list of drew cards</returns>
-        public IList<Card> Draw( uint num ) {
-            var r = new List<Card>( (int)num );
+        public IList<ICard> Draw( uint num ) {
+            var r = new List<ICard>( (int)num );
             for( int i = 0; i < num; i++ ) r.Add( Draw() );
             return r;
         }
@@ -81,27 +96,43 @@
         /// Reloads cards after battle, also Shuffles
         /// </summary>
         public void Reset() { 
-            Cards = new CardStack( _realcardsstructure );
+            Cards = new CardStack();
+
+            foreach( var c in GetStructure() ) Cards.AddTop(c);
+
             Shuffle();
         }
 
         public void Save() {
-            string s = "";
-            foreach (Card c in _realcardsstructure) { s += c.Code + "|"; }
-            s = s.Remove( s.Length - 1, 1 );
+            //string s = "";
+            //foreach (var c in _realcardsstructure) { s += c.Key.Code + "|"; }
+            //s = s.Remove( s.Length - 1, 1 );
 
+            //var con = new MySqlConnection( Com.connStr );
+            //con.Open();
+            //new MySqlCommand( "UPDATE `deck` SET `cards` = '" + s + "' WHERE `id` =" + _id, con).ExecuteNonQuery();
+            //con.Close();
             var con = new MySqlConnection( Com.connStr );
             con.Open();
-            new MySqlCommand( "UPDATE `deck` SET `cards` = '" + s + "' WHERE `id` =" + _id, con).ExecuteNonQuery();
+            foreach ( var c in _realcardsstructure ) {
+                new MySqlCommand( "REPLACE INTO `deck_cards` (`deckid`, `location`, `card`, `quantity`) VALUES ("+
+                    Id +", 'd', '"+ c.Key.Code +"', "+ c.Value +");", con ).ExecuteNonQuery();
+            }
             con.Close();
         }
 
-        public IEnumerator<Card> GetEnumerator() { return Cards.GetEnumerator(); }
+        public IEnumerator<ICard> GetEnumerator() { return Cards.GetEnumerator(); }
 
-        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
-            return Cards.GetEnumerator();
+        IEnumerator IEnumerable.GetEnumerator() { return Cards.GetEnumerator(); }
+
+        public IEnumerable<ICard> GetStructure() {
+            var l = new Queue<ICard>();
+
+            foreach ( var c in _realcardsstructure ) {
+                for( int i=0; i < c.Value; i++ ) l.Enqueue(c.Key);
+            }
+
+            return l;
         }
-
-        public IEnumerable<Card> GetStructure() { return _realcardsstructure; }
     }
 }