Fixed replace() bugs