fix variable replacement---needs to be improved