added comments and fixed minor bug