improved cd-wrapper function