code modified 2 implement correct replica trick