merge common graph copying code; move dead code elimination into an own file