defaultdict is probably better here, preventing double lookup and simplifying the code