rewrite to allow almost arbitrary substitution into keys and values