fix ordering of remove and replace; still very shaky though :-(