modified doRewriting method