Fixed some bugs, added some comments