Use const references to keys and values where appropriate to avoid copying them unnecessarily. Deleted some dead code. Simplified a few pieces that were doing unnecessary redundant work. Change-Id: Ib2145b7094a40db2d679e05dafe050fe1e87b846