Modified as exercise ch7