added comments and fixed bugs