this is messy because i went in like three unsuccessful directions before hitting on an approach that worked