added inline comments and minor modifications