fixed bugs and added comments