Formation and decomposition of magnesium hydride