slightly improved replace