refactor out keydist code