move dedup code into function, simplify