more effecient alloc of key &  val and also some misc cleaning up