Replicate is more robust

Previously, the replicate application was a bit hacked together and
not tested at all.  This change makes it much more robust than it was
and properly tests all of the code.