improve replacement algorithm