detailed codes and implementations