codes and detailed comment added.