better replace logic