Extended replace() and did some filesystem housekeeping