Andy first came up with the replacement idea as a way that he could be released from the cellar, having failed the test and found himself re-captured when Phelan was going to release him. Phelan initially refused the idea, but seems to have changed his mind since Daniel began sniffing about. Other than that, I'm as lost as you.