Support for auto swap.

Fixes #1339
Since this change is not small I would rather not add other changes