Cleanup replication #129

This is #129 rebased on top of the latest master.