Working randomized/deterministic base swap, with bugs corrected.