modified cp function