nit: you can avoid the lookup within the loop by using the value returned in the getOrElseUpdate calls.