Added comments. Some refectoring for better looking code