Fixed some bugs and added comments