Tests and more code cleanup.
Fixed bug with delayed replication events.