Fixed some bugs, added comments