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