Working version of replication.

Requires much needed cleanup and unit tests!!!