added more comments and minor formatting