Remove spagetti
Fix off-by-one in r_str_replace