fixed some bugs, added some comments