Replication

Big change, plus error message fix from other change before.

@enisoc @aaijazi