prepareGraph function, modified replace x