Fixed bugs and added comments