porting ed's changes from #994