Updated code based on BertrandBordage's review