added comments and minor formatting