Added replace helper function
Changed into general function