Added some comments and fixed some bugs