Porting @brylie's change with a small modification