this function could probably be streamlined a bit with `defaultdict` and some `or` statements but meh