refine code, merge dup code