Refactor merge_kwargs for clarity and to fix a few bugs
Merge pull request #1343 from gazpachoking/#1321

Refactor merge_kwargs