Fixed the bugs and added comments