Refactor merge_kwargs for clarity and to fix a few bugs