refracter code

mv dedup code and make responses outside of the critical section . And also, refracter the dedup code into a function.