implementing rewriting function