partial copy from @bjornmeansbear