added comments, tweaked code